import { bindable, containerless } from 'aurelia-framework';

@containerless
export class EmProjectEditVersion {

    @bindable project;
    versions = [];

    attached() {
        $(this.startRef).calendar({
            today: true,
            type: 'date',
            endCalendar: $(this.endRef)
        });
        $(this.endRef).calendar({
            today: true,
            type: 'date',
            startCalendar: $(this.startRef)
        });

    }

    projectChanged(newValue, oldValue) {
        if (newValue) {
            $.get('/admin/task/version/listByProject', {
                pid: this.project.id
            }, (data) => {
                if (data.success) {
                    this.versions = data.data;
                } else {
                    toastr.error(data.data);
                }
            });

        }
    }

    addHandler() {

    	if(!_.trim(this.name)) {
    		toastr.error('版本名称不能为空!');
    		return;
    	}

    	let data = {
    		pid: this.project.id,
            name: this.name,
            description: this.desc,
    	};
		let start = $(this.startRef).calendar('get date');
        let end = $(this.endRef).calendar('get date');

        if(start) {
        	data.start = start;
        }
        if(end) {
        	data.end = end;
        }

        $.post(`/admin/task/version/create`, data, (data) => {
            if (data.success) {
                this.versions.push(data.data);
                this.name = '';
                this.desc = '';
                $(this.startRef).calendar('clear');
                $(this.endRef).calendar('clear');
            } else {
                toastr.error(data.data);
            }
        });
    }

    delHandler(item) {
        $.post(`/admin/task/version/delete`, {
            id: item.id,
        }, (data) => {
            if (data.success) {
                this.versions = _.reject(this.versions, { id: item.id });
            } else {
                toastr.error(data.data);
            }
        });
    }

    editNameOkHandler(val, item) {
    	$.post(`/admin/task/version/update`, {
            id: item.id,
            name: val,
        }, (data) => {
            if (data.success) {
                toastr.success('更新成功!');
            } else {
                toastr.error(data.data);
            }
        });

    }

    editDescOkHandler(val, item) {
    	$.post(`/admin/task/version/update`, {
            id: item.id,
            description: val,
        }, (data) => {
            if (data.success) {
                toastr.success('更新成功!');
            } else {
                toastr.error(data.data);
            }
        });

    }

    editStartHandler(val, item) {
    	$.post(`/admin/task/version/update`, {
            id: item.id,
            start: val,
        }, (data) => {
            if (data.success) {
                toastr.success('更新成功!');
            } else {
                toastr.error(data.data);
            }
        });

    }

    editEndHandler(val, item) {
    	$.post(`/admin/task/version/update`, {
            id: item.id,
            end: val,
        }, (data) => {
            if (data.success) {
                toastr.success('更新成功!');
            } else {
                toastr.error(data.data);
            }
        });

    }
}

